package com.ayi.servlet.frontdesk; /**
 * @author Ayingzz
 * @create 2021/12/1 16:04
 */

import com.ayi.bean.User;
import com.ayi.dao.bookdao.UpdateBook;
import com.ayi.dao.cartdao.QueryCart;
import com.ayi.dao.cartdao.UpdateCart;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "updateBookNumFromCartServlet", value = "/updateBookNumFromCartServlet")
public class updateBookNumFromCartServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User)request.getSession().getAttribute("user");
        int id = Integer.parseInt(request.getParameter("id"));
        int num = Integer.parseInt(request.getParameter("num"));
        if (new UpdateCart().updateBookNumFromCart(user.getUsername(), id, num)) {
            new UpdateBook().updateInventory(id, -num);
            response.sendRedirect("IndexServlet");
        } else {

        }
    }
}
